package com.example.administrator.capital.item;


import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.capital.R;
import com.example.administrator.capital.adapter.CommentAdapter;
import com.example.administrator.capital.base.BaseBackFragment;
import com.example.administrator.capital.model.bean.DetailBean;
import com.example.administrator.capital.utils.CommonUtils;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import layout.LoadingLayout;
import recycle.BaseViewHolder;

/**
 * A simple {@link Fragment} subclass.
 * 还款信息
 */
public class RefundInformationFragment extends BaseBackFragment {
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R.id.swipe_layout)
    SwipeRefreshLayout swipeLayout;
    @BindView(R.id.loading)
    LoadingLayout loading;
    private CommentAdapter<DetailBean.DataBean.RepayListBean> mAdapter;
    Unbinder unbinder;
    private TextView mBack;

    public RefundInformationFragment() {
        // Required empty public constructor
    }

    public static RefundInformationFragment newInstance(Serializable list) {
        Bundle args = new Bundle();
        args.putSerializable("list", list);
        RefundInformationFragment fragment = new RefundInformationFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_refund_information, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mBack = view.findViewById(R.id.app_title_back);
        swipeLayout.setColorSchemeResources(R.color.font_blue);
        mAdapter = new CommentAdapter<DetailBean.DataBean.RepayListBean>(R.layout.support_item_list, null) {
            @Override
            public void setViewData(BaseViewHolder holder, DetailBean.DataBean.RepayListBean item, int position) {
                holder.setText(R.id.username, item.getNum()+"").setText(R.id.refundbz, item.getRewardamount()/100+"元")
                        .setText(R.id.repay_time, CommonUtils.transferLongToDate("yyyy-MM-dd", String.valueOf(item.getAddtime())));

            }
            @Override
            public void setEvent(BaseViewHolder holder, DetailBean.DataBean.RepayListBean item, int position) {
            }
        };
        recycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        DividerItemDecoration div = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        div.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.custom_divider));
        recycleView.addItemDecoration(div);
        recycleView.setAdapter(mAdapter);
    }
    @Override
    protected void setListener() {
        super.setListener();
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _mActivity.onBackPressed();
            }
        });
        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });
    }
    @Override
    protected void setData() {
        super.setData();
        getData();
    }
    private void getData() {
        mAdapter.setNewData((List<DetailBean.DataBean.RepayListBean>) mBundle.getSerializable("list"));
        loading.showContent();
        swipeLayout.setRefreshing(false);
    }
    @Override
    public void onClick(View view) {
        super.onClick(view);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
